package ru.spb.osll.myfit.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;

import java.util.Date;

import java.nio.channels.FileChannel;

import java.text.SimpleDateFormat;

import org.apache.log4j.Logger;

public class FileHelper {
	private Logger log = Logger.getLogger(FileHelper.class);

	public static final String SEPARATOR = "_";
	public static final String DATE_FORMAT = "yyyy-MMM-dd" + SEPARATOR + "HH-mm-ss-SSS";
	public static final String FORMAT_PATTERN = "^(\\S+)_(\\d\\d\\d\\d-\\S\\S\\S-\\d\\d_\\d\\d-\\d\\d-\\d\\d-\\d\\d\\d)$";

	public boolean createDir(String dirName) {
		File dir = new File(dirName);
		return dir.mkdirs();
	}

	public String createUDir(String where, String prefix) {
		SimpleDateFormat dateFormatter = new SimpleDateFormat(DATE_FORMAT);
		String newDirName = null;

		do {
			newDirName = where + File.separator +
					prefix + SEPARATOR + dateFormatter.format(new Date());
		} while(!createDir(newDirName));

		return newDirName;
	}

	public void copyDir(String srcDir, String dstDir) {
		File src = new File(srcDir);

		if(src.isDirectory())
			shareDir(src, dstDir);
	}

	private void shareDir(File srcDir, String dstDirName) {
		File[] children = srcDir.listFiles();

		for(File entry : children)
			if(entry.isDirectory())
				shareDir(entry, dstDirName + File.separator +
						entry.getName());
			else
				copyFile(entry.getPath(), dstDirName +
						File.separator + entry.getName());
	}

	public void copyFile(String srcFile, String dstFile) {
		File src = new File(srcFile);
		File dst = new File(dstFile);

		(new File(dst.getParent())).mkdirs();

		try {
			FileInputStream srcStream = new FileInputStream(src);
			FileOutputStream dstStream = new FileOutputStream(dst);

			FileChannel srcChannel = srcStream.getChannel();
			FileChannel dstChannel = dstStream.getChannel();
		
			srcChannel.transferTo(0, srcChannel.size(), dstChannel);

			srcChannel.close();
			dstChannel.close();

			dstStream.close();
			srcStream.close();
		} catch (FileNotFoundException e) {
			log.warn(e.getMessage(), e);
			e.printStackTrace();
		} catch (IOException e) {
			log.warn(e.getMessage(), e);
			e.printStackTrace();
		} catch (Exception e) {
			log.warn(e.getMessage(), e);
			e.printStackTrace();
		}	
	}

	public OutputStream openFile(String filename) throws FileNotFoundException {
		return new FileOutputStream(filename);
	}

	public InputStream openInputFile(String filename) throws FileNotFoundException {
		return new FileInputStream(filename);
	}

	public boolean isDirectory(File file) {
		return file.isDirectory();
	}

	public File getFile(String name) {
		return new File(name);
	}

	public File[] listFiles(File file) {
		return file.listFiles();
	}

	public String[] list(File file) {
		return file.list();
	}
	
	public boolean containPage(File dir) {
	  if (dir.isDirectory()) {
	    String[] files = dir.list();
	    for (String name : files)
	      if (name.endsWith(".html")) return true;
	  }
	  
	  return false;
	}
}
